package com.ruoyi.common.utils.jacob;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.io.File;
import java.io.IOException;

/**
 * @ClassName Word2Pdf
 * @Description TODO
 * @Author luguanqing
 * @Date 2021/3/25 14:11
 * @Version 1.0
 */
public class Word2Pdf {

    //static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
    static final int wdFormatPDF = 17;// word转PDF 格式

    public static void main(String[] args) throws IOException {
        String source1 = "D:\\111.docx";
        String target1 = "D:\\222.pdf";
        Word2Pdf w = new Word2Pdf();
        w.word2pdfUtil(source1, target1);
    }

    /**
     * EXCEL转PDF
     * @param source
     * @param target
     * @return
     */
    public boolean word2pdfUtil(String source, String target) {
        System.out.println("Word转PDF开始启动...");
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            ComThread.InitMTA(true);  //允许同时有多个WORD进程运行
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            System.out.println("打开文档：" + source);
            doc = Dispatch.call(docs, "Open", source).toDispatch();
            System.out.println("转换文档到PDF：" + target);
            doc = Dispatch.invoke(
                    docs,
                    "Open",
                    Dispatch.Method,
                    new Object[] { source, new Variant(false),
                            new Variant(true) }, new int[1]).toDispatch();
            File tofile = new File(target);
            if (tofile.exists()) {
                tofile.delete();
            }
            //Dispatch.call(doc, "SaveAs", target, wdFormatPDF);
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
                    target, new Variant(17) }, new int[1]);
            long end = System.currentTimeMillis();
            System.out.println("转换完成，用时：" + (end - start) + "ms");
            return true;
        } catch (Exception e) {
            System.out.println("Word转PDF出错：" + e.getMessage());
            return false;
        } finally {
            try{
                Dispatch.call(doc, "Close", false);
                if (app != null) {
                    app.invoke("Quit", new Variant[0]);
                }
            }
            finally{
                ComThread.Release();
            }
        }
    }


    public static boolean ex2PDF(String sourceFile, String target){
        final int xlTypePDF = 0;
        ComThread.InitSTA(true);
        ActiveXComponent ax = new ActiveXComponent("Excel.Application");
        try{
        long date = System.currentTimeMillis();
        ax.setProperty("Visible", new Variant(false));
        ax.setProperty("AutomationSecurity", new Variant(3));
        Dispatch excels = ax.getProperty("Workbooks").toDispatch();
        Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method, new Object[] { sourceFile, new Variant(false), new Variant(false) },
                new int[9]).toDispatch();
        File tofile = new File(target);
        if (tofile.exists()) {
            tofile.delete();
        }
        // 转换格式
        Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[] {new Variant(0), // PDF格式=0
                target, new Variant(xlTypePDF), Variant.VT_MISSING, Variant.VT_MISSING, Variant.VT_MISSING, Variant.VT_MISSING,
                new Variant(false), Variant.VT_MISSING }, new int[1]);
        long date2 = System.currentTimeMillis();
        int time = (int) ((date2 - date) / 1000);
        Dispatch.call(excel, "close", new Variant(false));
        System.out.println("转换完成，用时：" + time + "s");
        return true;
        } catch (Exception e) {
            System.out.println("Word转PDF出错：" + e.getMessage());
            return false;
        } finally {
            if (ax != null) {
                ax.invoke("Quit", new Variant[] {});
            }
            ComThread.Release();
        }
    }

}
